﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Soft
{

    /// <summary>
    /// 错误响应
    /// </summary>
    public abstract class ErrorResponse
    {

        /// <summary>
        /// 错误处理
        /// </summary>
        public event ErrorEventHandle Error;


        /// <summary>
        /// 错误处理
        /// </summary>
        /// <param name="result"></param>
        protected virtual void OnError(IResult result)
        {
            if (result != null && !result.Succeed)
            {
                this.OnError(result.Error);
            }
        }


        /// <summary>
        /// 错误处理
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnError(Exception e)
        {
            this.OnError(e.ToString() + Environment.NewLine);
        }


        /// <summary>
        /// 错误处理
        /// </summary>
        /// <param name="error"></param>
        protected virtual void OnError(string error)
        {
            this.OnError(new Information(InformationType.Error, error, this));
        }


        /// <summary>
        /// 错误处理
        /// </summary>
        /// <param name="informations"></param>
        protected virtual void OnError(InformationCollection informations)
        {
            if (informations == null)
            {
                throw new ArgumentNullException("informations");
            }
            StringBuilder stringBuilder = new StringBuilder();
            foreach (Information information in informations)
            {
                stringBuilder.AppendLine(information.Text);
            }
            this.OnError(stringBuilder.ToString());
        }


        /// <summary>
        /// 错误处理
        /// </summary>
        /// <param name="error"></param>
        protected virtual void OnError(Information error)
        {
            ErrorEventHandle error2 = this.Error;
            if (error2 == null)
            {
                return;
            }
            error2(error);
        }



    }
}
